//package com.sunday.framework.jwt.userdetails;
//
//import com.sunday.common.core.constants.LogConstants;
//import com.sunday.common.core.enums.ErrorCodeEnum;
//import com.sunday.common.core.exception.BusinessException;
//import com.sunday.common.core.gson.GsonUtils;
//import com.sunday.common.core.remoting.rest.annotation.OpenApi;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.web.context.request.RequestAttributes;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//
//import java.util.Optional;
//
///**
// * @Description: 当前用户持有者
// * @Author: zsy
// * @Create: 2022/11/16 14:33
// */
//@Slf4j
//public class CurrentUserHolder {
//
//    private static final ThreadLocal<UserInfo> contextHolder = new ThreadLocal<>();
//
//    /**
//     * @Description: 通过 请求头中的 tokenInfo 绑定当前登录人信息
//     * @Param: [openApi 为null时，则默认进行绑定动作 ]
//     * @Return: void
//     * @Author: zsy
//     * @Time: 2022/11/16 22:45
//     */
//    public static void bind(OpenApi openApi) {
//        //接口是否公开，null 不公开，必须验证用户信息
//        if (openApi == null) {
//            RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
//            log.info("{}", attributes);
//            contextHolder.set(
//                    Optional.ofNullable(RequestContextHolder.getRequestAttributes())
//                            .filter(requestAttributes -> requestAttributes instanceof ServletRequestAttributes)
//                            .map(requestAttributes -> ServletRequestAttributes.class.cast(requestAttributes)
//                                    .getRequest()
//                                    .getHeader(UserInfo.HTTP_HEADER_AUTH_TOKEN_INFO))
//                            .map(CurrentUserHolder::getUserInfo)
//                            /** A0_200(ErrorClassifyEnum.A + "0200","用户登录异常") **/
//                            .orElseThrow(() -> new BusinessException(ErrorCodeEnum.A0_200))
//            );
//            log.info(LogConstants.bind_format, contextHolder.get());
//        }
//    }
//
//    /**
//     * @Description: 通过token获取用户信息
//     * @Param: [token]
//     * @Return: com.sunday.sso.resource.jwt.userdetails.UserInfo
//     * @Author: zsy
//     * @Time: 2022/11/16 21:51
//     */
//    public static UserInfo getUserInfo(String token) {
//        return Optional.ofNullable(token)
//                .filter(StringUtils::isNotBlank)
//                .map(tokenInfo -> GsonUtils.DEFAULT.fromJson(tokenInfo, UserInfo.class))
//                .filter(UserInfo::validation)
//                .orElse(null);
//    }
//
//    /**
//     * @Description: 获取当前登录人信息， 如果获取不到返回异常
//     * @Param: []
//     * @Return: com.sunday.sso.resource.jwt.userdetails.UserInfo
//     * @Author: zsy
//     * @Time: 2022/11/16 22:44
//     */
//    public static UserInfo getUserInfo() {
//        return Optional.ofNullable(contextHolder.get())
//                /** A0_200(ErrorClassifyEnum.A + "0200","用户登录异常") **/
//                .orElseThrow(() -> new BusinessException(ErrorCodeEnum.A0_200));
//    }
//
//    /**
//     * @Description: 解除绑定
//     * @Param: []
//     * @Return: void
//     * @Author: zsy
//     * @Time: 2022/11/16 22:45
//     */
//    public static void unbind() {
//        contextHolder.remove();
//    }
//
//}
